package cn.xfdzx.android.apps.shop.adapter;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.bean.VideoRoomGoodListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.util.Utils;

/**
 * @author ln
 * @date 2020/3/27
 * description：
 */
public class VideoGoodListAdapter extends RecyclerView.Adapter<VideoGoodListAdapter.MyViewHolder> {
    private ItemClickListener mItemClickListener;
    private List<VideoRoomGoodListBeen.DataBean.GoodsBean> mList;
    private Context mContext;
    private  int now_position=0;
    Dialog dialogEdit;
    private  EditText ettGoodPrice;
    private EditText ettGoodNowPrice;
    private EditText ettKucun;



    public VideoGoodListAdapter(Context context, List<VideoRoomGoodListBeen.DataBean.GoodsBean> list,   Dialog dialogEdit, EditText goodPrice,EditText goodNowPrice,EditText kucun) {
        this.mList = list;
        this.mContext = context;

        this.dialogEdit = dialogEdit;
        this.ettGoodPrice = goodPrice;
        this.ettGoodNowPrice = goodNowPrice;
        this.ettKucun = kucun;
    }

    public int getSelectPosition(){
        return now_position;
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_video_room_good_list, parent, false);
        MyViewHolder myHolder = new MyViewHolder(inflate);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
        RoundedCorners roundedCorners = new RoundedCorners(20);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
        Glide.with(mContext).load(mList.get(position).getThumb()).apply(options.transform(new CenterCrop(), roundedCorners)).into(holder.ivGoodImage);
        holder.tvGoodName.setText(mList.get(position).getTitle());
        holder.tvStock.setText("库存:" + mList.get(position).getStock() + "");
        if (Double.parseDouble(mList.get(position).getDiscountprice())==0){
            holder.tvGoodPrice.setText("¥" + mList.get(position).getMarketprice() + "");
        }else {
            holder.tvGoodPrice.setText("¥" + mList.get(position).getDiscountprice() + "");
        }
        holder.tvJiang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        holder.tvEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isFastClick()) {
                    now_position = position;
                    ettGoodPrice.setText(mList.get(now_position).getMarketprice() + "");
                    ettGoodNowPrice.setText(mList.get(now_position).getDiscountprice() + "");
                    ettKucun.setText(mList.get(now_position).getStock() + "");
                    dialogEdit.show();
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_good_image)
        ImageView ivGoodImage;
        @BindView(R.id.tv_good_name)
        TextView tvGoodName;
        @BindView(R.id.tv_stock)
        TextView tvStock;
        @BindView(R.id.tv_good_price)
        TextView tvGoodPrice;
        @BindView(R.id.tv_jiang)
        TextView tvJiang;
        @BindView(R.id.tv_edit)
        TextView tvEdit;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}
